package com.xiguaji.tiktok.task.monitor;

import cn.hutool.core.date.DateUtil;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xxl.job.core.log.XxlJobFileAppender;
import com.xxl.job.core.log.XxlJobLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;
import java.io.File;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;


@Configuration
public class LogUtil {

    private static RedisTemplate<String,String> redis;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @PostConstruct
    public void setRedis(){
        redis = redisTemplate;
    }

    public static final InheritableThreadLocal<String> contextHolder = new InheritableThreadLocal();


    public static String getLogId(){
        String logDesc = XxlJobFileAppender.contextHolder.get();
        return logDesc.substring(logDesc.lastIndexOf(File.separatorChar)+1,logDesc.lastIndexOf("."));
    }

    private static String getKey(){
        return LogUtil.contextHolder.get();
    }

    public static Monitor getLogData(){
        if(null == LogUtil.getKey()){
            return null;
        }

        try{
            String data = redis.opsForValue().get(LogUtil.getKey());
            return JsonUtil.jsonParse(Monitor.class,data);
        }catch (Exception e){
            XxlJobLogger.log("redis error key:{}  value:{}",LogUtil.getKey(),redis.opsForValue().get(LogUtil.getKey()));
        }
        return null;
    }

    public static void setLogData(Monitor monitor){
        if(null == LogUtil.getKey()){
            return;
        }
        monitor.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        redis.opsForValue().set(LogUtil.getKey(),JsonUtil.parseJson(monitor));
    }

    public static void delLogData(){
        Monitor monitor = getLogData();

        if(null == monitor){
            return;
        }

        LocalDateTime endTime = DateUtil.parseLocalDateTime(monitor.getUpdateTime(),"yyyy-MM-dd HH:mm:ss");
        LocalDateTime estimateTime = DateUtil.parseLocalDateTime(monitor.getEstimateTime(),"yyyy-MM-dd HH:mm:ss");

        if(endTime.atZone(ZoneId.systemDefault()).toEpochSecond() <= estimateTime.atZone(ZoneId.systemDefault()).toEpochSecond()){
            redis.delete(LogUtil.getKey());
        }
    }

    public static void setCountSize(int countSize){
        Monitor monitor = getLogData();

        if(null == monitor){
            return;
        }
        monitor.setCountSize(countSize);
        monitor.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        redis.opsForValue().set(LogUtil.getKey(),JsonUtil.parseJson(monitor));
    }

    public static void addCountSize(int countSize){
        Monitor monitor = getLogData();

        if(null == monitor){
            return;
        }
        monitor.setCountSize(monitor.getCountSize()+countSize);
        monitor.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        redis.opsForValue().set(LogUtil.getKey(),JsonUtil.parseJson(monitor));
    }

    public static void setCurSize(int curSize){
        Monitor monitor = getLogData();

        if(null == monitor){
            return;
        }
        monitor.setCurSize(curSize);
        monitor.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        redis.opsForValue().set(LogUtil.getKey(),JsonUtil.parseJson(monitor));
    }

    public static void addCurSize(int curSize){
        Monitor monitor = getLogData();

        if(null == monitor){
            return;
        }
        monitor.setCurSize(monitor.getCurSize()+curSize);
        monitor.setUpdateTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        redis.opsForValue().set(LogUtil.getKey(),JsonUtil.parseJson(monitor));
    }

    public static void inheritableThreadLocal(){
        contextHolder.remove();
    }
}
